* Balance vars
  #delimit ;
  local balance_vars = " age ado bmi 
    cohabiting married 
    tertiary secondary primary
    salaried self_employed student
    pregnancies children delivery_ever delivery_3mless   
    spacing_nomore spacing_less3yrs spacing_more3yrs 
    current_larc current_sarc current_nonmc mim" ;
  #delimit cr

* Load data
  use "${PilotPublicData}/hgopy_followup_public.dta", clear 
    
  keep `balance_vars' 
  gen group = 0
  
  tempfile fup_data
  save `fup_data'
  
  
** Who are the clients in the groups 
  local groups = " depfp " 
*local with the balance variables
  #delimit ;
  local balance_vars = " age ado bmi 
    cohabiting married 
    tertiary secondary primary
    salaried self_employed student
    pregnancies children delivery_ever delivery_3mless   
    spacing_nomore spacing_less3yrs spacing_more3yrs 
    current_larc current_sarc current_nonmc mim" ;
  #delimit cr

*load dataset
  use "${PilotPublicData}/hgopy_pilot_public.dta", clear

  keep `balance_vars' 
  gen group = 1
  
* append datasets
  append using `fup_data'
  
* Fix group variable
  label define group 0"FUP Data" 1"Pilot Data"
  label values group group
  
  
* Balance Table  
  
*get list of binary vars
  sum `balance_vars'
  local binary = "" 
  local continuous = ""
  qui foreach v of varlist `balance_vars' {
    su `v' , 
    if (`r(min)'==0 & `r(max)'==1) {
      local binary = "`binary' `v'"
    }
    else if !(`r(min)'==0 & `r(max)'==1) {
      local continuous = "`continuous' `v'"
    }
  }
* load local with variable labels
  qui foreach v of local balance_vars {
    local varlab_`v' : variable label `v'
  }
* create the table
  iebaltab `balance_vars', grpvar(group) control(1) order(1 0)  ///
    browse rowvarlabels tblnonote format(%9.2f) onerow pt std starsnoadd 
    
* Clean up
  tempfile x
  rename * y*
  save `x'
* Clean table
  egen x = fill( 0 1 0 1)
  replace x = . if _n<=3
    
  foreach v of varlist yv2 yv3 yv4 {
    gen x`v' = `v'[_n+1] if x==1
    order x`v', after(`v')
  }
  drop if yv1=="" & !missing(x)
  drop x
*clean up parentheses
  qui foreach v of varlist x* {
    replace `v'=subinstr(`v',"[","(",.)
    replace `v'=subinstr(`v',"]",")",.)	
  }
  
* Remove parentheses for dummies
  qui foreach v of local binary {
    foreach w of varlist yv2 yv3 {
      replace x`w'="" if yv1=="`varlab_`v''"
    }
  }	 
  qui foreach v of local continuous {
    foreach w of varlist yv2 yv3 {
      replace `w'=`w'+x`w' if yv1=="`varlab_`v''"
    }
  }
* Drop cols and rows that we don't need 
  drop x*
  drop if _n<4  
* Add commas
  foreach var of varlist yv3 yv4 {
    replace `var' = substr(`var', 1,1) + ","+ substr(`var',2,.) ///
      if strlen(`var') >=4 & _n == _N
  }
  
* drop the p-values
  drop yv4
  
* save
  #delimit ;
  texsave_custom using "${OutTexTab}/TabS1-descriptives_pilot_fup_data.tex" , replace nonames  hlines(22) frag nofix
    align(" l*{3}{c} ")
    headerlines(" & (1) & (2)  "
                " & \multicolumn{1}{c}{\textbf{Pilot Data}} & \multicolumn{1}{c}{\textbf{FUP Data}} " 
                " & Mean/(SD) & Mean/(SD) ") ;
  #delimit cr

   